home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Pascal / Applications / TCPExample / PNL Libraries / MyStartup.p < prev    next >
Text File  |  1997-03-20  |  5KB  |  228 lines

  1. unit MyStartup;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types,
  7.         MyAssertions;
  8.     
  9.     var
  10.         current_time: longint;
  11.         
  12.     type
  13.         StartupMessages = (SMT_None, SMT_Startup, SMT_Generic, SMT_NeedsSystem7, SMT_FailedToInitTCP, SMT_Rest);
  14.         StartupInitProc = function(var msg: integer): OSStatus;
  15.         StartupIdleProc = procedure;
  16.         StartupFinishProc = procedure;
  17.         
  18. {$ifc not do_debug}
  19. {$definec DidStartup(b) if false then begin end else begin end }
  20. {$definec AssertDidStartup(b) if false then begin end else begin end }
  21. {$elsec}
  22. {$definec DidStartup(b) DidStartupCode(b)}
  23. {$definec AssertDidStartup(b) AssertDidStartupCode(b)}
  24. {$endc}
  25.  
  26.     procedure InitStartup;
  27.     procedure SetStartup(init: StartupInitProc; idle: StartupIdleProc; idle_period: longint; finish: StartupFinishProc);
  28.     function Startup(var msg: integer): OSStatus;
  29.     procedure IdleStartup;
  30.     procedure FinishStartup;
  31.     procedure FireAtIdle(idle: StartupIdleProc);
  32. {$ifc do_debug}
  33.     procedure DidStartupCode( var check: integer );
  34.     procedure AssertDidStartupCode( check: integer );
  35. {$endc}    
  36.     
  37. implementation
  38.  
  39.     uses
  40.         Memory, Events, MyMemory;
  41.     
  42.     type
  43.         EntryRecord = record
  44.             init: StartupInitProc;
  45.             idle: StartupIdleProc;
  46.             idle_period: longint;
  47.             next_idle: longint;
  48.             finish: StartupFinishProc;
  49.         end;
  50.         EntryArray = array[1..10000] of EntryRecord;
  51.         EntryPtr = ^EntryArray;
  52.         EntryHandle = ^EntryPtr;
  53.  
  54.     const
  55.         idles_max = 100;
  56.         did_startup_check = $5748;
  57.         
  58. {$ifc do_debug}
  59.     var
  60.         startup_check: integer;
  61. {$endc}
  62.  
  63.     var
  64.         idles: array[1..idles_max] of StartupIdleProc;
  65.         max_idles: longint;
  66.         startup_error: OSStatus;
  67.         entries: EntryHandle;
  68.         entries_count: longint;
  69.     
  70. {$ifc do_debug}
  71.     procedure DidStartupCode( var check: integer );
  72.     begin
  73.         check := did_startup_check;
  74.     end;
  75.     
  76.     procedure AssertDidStartupCode( check: integer );
  77.     begin
  78.         Assert( check = did_startup_check );
  79.     end;
  80. {$endc}
  81.  
  82.     procedure FireAtIdle(idle: StartupIdleProc);
  83.         var
  84.             i: longint;
  85.             found: Boolean;
  86.             hack: StartupIdleProc;
  87.     begin
  88.         found := false;
  89.         for i := 1 to idles_max do begin
  90.             hack := idles[i];
  91.             if hack= nil then begin
  92.                 idles[i] := idle;
  93.                 if i > max_idles then begin
  94.                     max_idles := i;
  95.                 end;
  96.                 found := true;
  97.                 leave;
  98.             end;
  99.         end;
  100.         if not found then begin
  101.             idle();
  102.         end;
  103.     end;
  104.     
  105.     procedure SetStartup(init: StartupInitProc; idle: StartupIdleProc; idle_period: longint; finish: StartupFinishProc);
  106.         var
  107.             found: Boolean;
  108.             entry: EntryRecord;
  109.             i: longint;
  110.     begin
  111.         if (startup_error = noErr) & (entries = nil) then begin
  112.             startup_error := MNewHandle(entries, 0);
  113.         end;
  114.         if startup_error = noErr then begin
  115.             found := false;
  116.             for i := 1 to entries_count do begin
  117.                 if (entries^^[i].init = init) & (entries^^[i].idle = idle) & (entries^^[i].idle_period = idle_period) & (entries^^[i].finish = finish) then begin
  118.                     found := true;
  119.                     leave;
  120.                 end;
  121.             end;
  122.             if not found then begin
  123.                 entry.init := init;
  124.                 entry.idle := idle;
  125.                 entry.idle_period := idle_period;
  126.                 entry.next_idle := TickCount;
  127.                 entry.finish := finish;
  128.                 startup_error := PtrAndHand(@entry, Handle(entries), SizeOf(entry));
  129.                 if startup_error = noErr then begin
  130.                     Inc(entries_count);
  131.                 end;
  132.             end;
  133.         end;
  134.     end;
  135.     
  136.     procedure IdleStartup;
  137.         var
  138.             i: longint;
  139.             tmp_hack: StartupIdleProc;
  140.     begin
  141.         current_time := TickCount;
  142.         for i := 1 to entries_count do begin
  143.             tmp_hack := entries^^[i].idle;
  144.             if (tmp_hack <> nil) & (current_time >= entries^^[i].next_idle) then begin
  145.                 entries^^[i].next_idle := current_time + entries^^[i].idle_period;
  146.                 tmp_hack;
  147.             end;
  148.         end;
  149.         for i := 1 to max_idles do begin
  150.             tmp_hack := idles[i];
  151.             if (tmp_hack<> nil) then begin
  152.                 tmp_hack();
  153.                 idles[i] := nil;
  154.             end;
  155.         end;
  156.     end;
  157.     
  158.     procedure InitStartup;
  159.         var
  160.             i: longint;
  161.     begin
  162.         DidStartup( startup_check );
  163.         entries := nil;
  164.         entries_count := 0;
  165.         startup_error := noErr;
  166.         max_idles := 0;
  167.         for i := 1 to idles_max do begin
  168.             idles[i] := nil;
  169.         end;
  170.     end;
  171.     
  172.     function Startup(var msg: integer): OSStatus;
  173.         var
  174.             i: longint;
  175.             tmp_hack: StartupFinishProc;
  176.             tmp_hack_init: StartupInitProc;
  177.     begin
  178.         AssertDidStartup( startup_check );
  179.         msg := ord(SMT_Startup);
  180.         i := 0;
  181.         while (startup_error = noErr) & (i < entries_count) do begin
  182.             i := i + 1;
  183.             tmp_hack_init := entries^^[i].init;
  184.             if tmp_hack_init <> nil then begin
  185.                 msg := ord(SMT_Generic);
  186.                 startup_error := tmp_hack_init(msg);
  187.             end;
  188.         end;
  189.         if startup_error <> noErr then begin
  190.             i := i - 1;
  191.             while i > 0 do begin
  192.                 tmp_hack := entries^^[i].finish;
  193.                 if tmp_hack <> nil then begin
  194.                     tmp_hack;
  195.                 end;
  196.                 i := i - 1;
  197.             end;
  198.             MDisposeHandle(entries);
  199.             entries_count := 0;
  200.         end;
  201.         if startup_error = noErr then begin
  202.             msg := ord(SMT_None);
  203.         end;
  204.         Startup := startup_error;
  205.     end;
  206.     
  207.     procedure FinishStartup;
  208.         var
  209.             i: longint;
  210.             tmp_hack: StartupFinishProc;
  211.     begin
  212.         AssertDidStartup( startup_check );
  213.         if entries <> nil then begin
  214.             i := entries_count;
  215.             while i > 0 do begin
  216.                 tmp_hack := entries^^[i].finish;
  217.                 if tmp_hack <> nil then begin
  218.                     tmp_hack;
  219.                 end;
  220.                 i := i - 1;
  221.             end;
  222.             MDisposeHandle(entries);
  223.             entries_count := 0;
  224.         end;
  225.     end;
  226.  
  227. end.
  228.